<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            background: #000;
            text-align: center
        }

        #c1 {
            background: #fff;
        }
    </style>

</head>

<body>
    <canvas id="c1" width="800" height="600">

    </canvas>
</body>
<script>
    let oC = document.getElementById('c1');
    let gd = oC.getContext('2d');       //图形上下文-绘图用的接口
    class Background{
        constructor(x,y,width,height,img_src){
            this.x=x;
            this.y=y;
            this.width=width;
            this.height=height;
            let img=new Image();
            img.src=img_src;
            this.img=img;
        }
        draw(){
            this.img.onload=()=>{
                gd.drawImage(this.img, this.x, this.y, this.width, this.height);
            }
        }
    }
    
    let bg=new Background(10,10,300,300,'./canyu.png');
    bg.draw()
    
</script>

</html>